package com.vacomall.spring.jfinal;

import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.lang3.Validate;
import org.apache.log4j.Logger;

import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.dialect.Dialect;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.vacomall.spring.jfinal.annotaion.Table;
import com.vacomall.spring.jfinal.util.PackageUtil;
/**
 * Spring注入Jfinal Record bean
 * @author gaojun.zhou
 * @date 2016年8月8日
 *
 */
public class JfinalActiveRecordPluginBean {
	
	public static final Logger loger = Logger.getLogger(JfinalActiveRecordPluginBean.class);
	private ActiveRecordPlugin activeRecordPlugin;
	/**数据源**/
	private DataSource dataSource;
	/**需要扫描的包**/
	private String entityScannerBasePackage;
	/**事物级别**/
	private Integer transactionLevel = 4;
	/**打印SQL**/
	private Boolean showSql = false;
	/**开启开发模式**/
	private Boolean devMode = false;
	/**方言**/
	private Dialect dialect = null;
	
	@SuppressWarnings("unchecked")
	public void init(){
		
		if(activeRecordPlugin == null){
			
			activeRecordPlugin = new ActiveRecordPlugin(dataSource);
			activeRecordPlugin.setTransactionLevel(transactionLevel);
			activeRecordPlugin.setShowSql(showSql);
			activeRecordPlugin.setDevMode(devMode);
		}
		/**
		 * 设置方言
		 */
		if(dialect!=null){
			activeRecordPlugin.setDialect(dialect);
		}else{
			activeRecordPlugin.setDialect(new MysqlDialect());
		}
		
		Validate.notNull(activeRecordPlugin);
		Validate.notBlank(entityScannerBasePackage);
		
		
		List<String> basePkgList = this.splitEntityScannerBasePackage(this.entityScannerBasePackage);
		
		for(String pkgName : basePkgList){
			
			List<String> entitys = PackageUtil.getClassName(pkgName, true);
			
			for(String entity : entitys){
				
				try {
					
					Class<?>  clazz = Class.forName(entity);
					Table table = (Table) clazz.getAnnotation(Table.class);
					activeRecordPlugin.addMapping(table.tableName(),table.pk(),(Class<? extends Model<?>>) Class.forName(entity));
					
					loger.debug("model:"+entity+",table:"+table.tableName()+",pk:"+table.pk());
					
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
			
		}
		
		activeRecordPlugin.start();	
	}
	
	public void stop(){
		
		if(activeRecordPlugin!=null){
			activeRecordPlugin.stop();
		}
		
	}

	
	/**
	 * 用逗号窃取包路径
	 * @param entityScannerBasePackage
	 * @return
	 */
	private List<String>  splitEntityScannerBasePackage(String entityScannerBasePackage){
		
		Validate.notBlank(entityScannerBasePackage);
		
		List<String> list  = new ArrayList<String>();
		
		String[] basePackages = entityScannerBasePackage.split(";");
		
		for(String pkg : basePackages){
			
			list.add(pkg);
			
		}
		
		return list;
	}

	
	public String getEntityScannerBasePackage() {
		return entityScannerBasePackage;
	}

	public void setEntityScannerBasePackage(String entityScannerBasePackage) {
		this.entityScannerBasePackage = entityScannerBasePackage;
	}
	
	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public Integer getTransactionLevel() {
		return transactionLevel;
	}

	public void setTransactionLevel(Integer transactionLevel) {
		this.transactionLevel = transactionLevel;
	}

	public Boolean getShowSql() {
		return showSql;
	}

	public void setShowSql(Boolean showSql) {
		this.showSql = showSql;
	}

	public Boolean getDevMode() {
		return devMode;
	}

	public void setDevMode(Boolean devMode) {
		this.devMode = devMode;
	}

	public Dialect getDialect() {
		return dialect;
	}

	public void setDialect(Dialect dialect) {
		this.dialect = dialect;
	}
	
	
}
